using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Interactivity;
using System;
using testapp.ViewModels;
using testapp.Models;
using SCTO.Center;
using System.Linq;

namespace testapp.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
#if DEBUG
            this.AttachDevTools();
#endif
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        private void CloseFileDetail(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            vm.FileDetailOpened = false;
        }

        private void OnDownload(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            if (vm.SelectedClient is null) return;
            SctoModel.SctoModelInstance.DownloadFile(vm.SelectedClient, vm.FileDetailName);
            vm.FileDetailOpened = false;
        }

        private void OnRefresh(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            vm.FileList = (SctoFileInfo.ToViewModel(SctoModel.SctoModelInstance.GetFiles())).ToList();
        }

        private void FileSelected(object sender, SelectionChangedEventArgs e)
        {
            var vm = DataContext as MainWindowViewModel;
            vm.FileDetailName = (e.AddedItems?[0] as FileInfoViewModel)?.FileName;
            if (vm.FileDetailName is null) return;
            vm.ClientInfos = SctoModel.SctoModelInstance.GetFileInfo(vm.FileDetailName).ToList();
            vm.FileDetailOpened = true;
        }
    }
}
